package com.basker.pisces.core.query.factory;

import java.util.ArrayList;
import java.util.List;

import com.basker.pisces.core.query.builder.CriterionBuilder;
import com.basker.pisces.utils.ListsUtils;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public abstract class AbstractQueryInfo {

    /**
     * 是否需要查询引用字段，比如关联表对象对应的代码和名称字段
     */
    private boolean selectReferenceFields = true;

    /**
     * 查询条件
     */
    private List<Condition> conditions = new ArrayList<>();

    /**
     * 排序
     */
    private List<Order> orders = new ArrayList<>();

    /**
     * 添加一个条件
     *
     * @param field
     * @param operator
     * @param value
     * @see Condition
     */
    public void addCondition(String field, String operator, Object value) {
        this.conditions.add(new Condition(field, operator, value));
    }

    /**
     * 添加一个排序
     *
     * @param field
     * @param desc
     */
    public void addOrder(String field, boolean desc) {
        this.orders.add(new Order(field, desc));
    }

    /**
     * 将condition对象构建成CriterionBuilder对象，后者可以提供给Repository接口使用
     *
     * @return
     */
    public List<CriterionBuilder> buildCriterionBuilder() {
        return ListsUtils.mapNotNull(conditions, Condition::createCriterionBuilder);
    }
}
